%% SENSARIS DATA ANALYSIS script
% This script defines and initializes the variables.

%% cleaning
close all;

%% global variables
global BASE_DIRECTORY;
global NOX_SCALING;
global COX_SCALING;
global HUM_SCALING;
global TEMP_SCALING;
global NOISE_SCALING;
global DEVICE_IDS;

%% Extract data from sensor devices
% deviceId = 'A';
% testDeviceStatus(deviceId, FILES_DIRECTORY);
% downloadFilesFromDevice(deviceId, FILES_DIRECTORY);

% move files to temp directory
% yymmdd.LOG => A_yymmdd.LOG

%% pre-proces log files
%
% Create a *.mat file for each *.log file.
files = {};
%files = dir('*.LOG');

nfiles = size(files,1);
%files_to_process=[];
disp(['Files to be processed:',num2str(nfiles)]);
for i=1:nfiles
%    files_to_process=[files_to_process; {files(i).name}];
    filename = files(i).name;
    disp(['Processing ...:', filename]);
    values= parse_sensaris_logfile(filename);
    matfilename = strcat(filename,'.mat');
    save(matfilename, 'values');
end

%% select data for graphs
%
% Generate an 
%   sensorId, timestamp, humidity, temperature, NOx, COx, noise1, noise2, gps1, ..., gps10, battery

start_date=datenum('2010/04/28 08:00:00','yyyy/mm/dd HH:MM:SS');
end_date=datenum('2010/04/29 12:00:00','yyyy/mm/dd HH:MM:SS');

filename=[datestr(start_date, 'yymmdd'),'_C.LOG.mat'];
load(filename);
disp(['Loaded:', filename]);


%devices = {'A', 'B'};
%location= [latitude, longitude, radius];
%sensorData = [];

%% plot
% T,H,NOx,COx
sensorData = [values.data(:,2)*TEMP_SCALING values.data(:,3)*HUM_SCALING ...
    values.data(:,4)*NOX_SCALING values.data(:,5)*COX_SCALING];

h = figure ('Name', ['Sensaris: ', filename] );
hold on;
ylabel('Normalized scale');
legend_names = {};

color = get_a_new_color();
plot(sensorData(:,1), 'color',color);
legend_names = [ legend_names, {'T'} ];

color = get_a_new_color();
plot(sensorData(:,2), 'color',color);
legend_names = [ legend_names, {'H'} ];

color = get_a_new_color();
plot(sensorData(:,3), 'color',color);
legend_names = [ legend_names, {'NOx'} ];

color = get_a_new_color();
plot(sensorData(:,4), 'color',color);
legend_names = [ legend_names, {'COx'} ];

legend (legend_names); %legend should be added after plots


%% display data
%----------------------------------------
% ONE SENSOR:
% Display the data collected by a sensor.
%----------------------------------------
%plot_multiple(sensorData);

%----------------------------------------
% TIME-EVOLUTION
% Display the evolution in time in a
% selected area (data from different sensors).
%----------------------------------------

%----------------------------------------
% SPACE
% Display the distribution in a 2-D map.
%----------------------------------------

%----------------------------------------
% KML views
%----------------------------------------
% Display trajectory with sensed data.
% Display a gradient map (animation in time).
